package ba.unsa.etf.pikks.forme;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import ba.unsa.etf.pikks.LoC.LOC;
import ba.unsa.etf.pikks.mccabe.McCabe;

import javax.swing.border.TitledBorder;
import java.awt.Component;

public class MainForma extends JFrame {

	private JPanel contentPane;
	private JTextField putanja;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainForma frame = new MainForma();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainForma() {
		setTitle("Kod analizator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		
		
		setBounds(100, 100, 483, 575);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		final JTextArea ispis = new JTextArea();
		ispis.setWrapStyleWord(true);
		ispis.setAlignmentX(Component.LEFT_ALIGNMENT);
		ispis.setBounds(4, 93, 463, 433);
		ispis.setMinimumSize(new Dimension(100, 100));
		ispis.setPreferredSize(new Dimension(300, 400));
		ispis.setRows(10);
		getContentPane().add(ispis);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Otvori izvorni kod", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 5, 438, 83);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton = new JButton("Otvori Fajl");
		btnNewButton.setBounds(10, 21, 97, 23);
		panel.add(btnNewButton);
		
		putanja = new JTextField();
		putanja.setBounds(117, 22, 311, 20);
		panel.add(putanja);
		putanja.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("McCabe");
		btnNewButton_1.setBounds(257, 49, 71, 23);
		panel.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("LoC Counter");
		btnNewButton_2.setBounds(335, 49, 93, 23);
		panel.add(btnNewButton_2);
		
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				HashMap<String,Integer> resultMap = new HashMap<String, Integer>();
		    	LOC loccounter = new LOC();
				File sourceFile = new File(putanja.getText());
				try {
					resultMap = (HashMap<String, Integer>) loccounter.LocCounterManager(sourceFile);
				} catch (Exception e) {
					e.printStackTrace();
				}	
				
				String methodReport="\nLOC od metoda:";
				String classReport="";
				for(String methodName: resultMap.keySet()){
					if(methodName=="Class"){
						String name=sourceFile.getName();
						String className=name.substring(0, name.indexOf("."));		
						classReport+="LOC klase "+ className;
						classReport+=" je "+resultMap.get("Class");
					}
					else{
						methodReport+="\nMetoda:	"+methodName;
						methodReport+="\nLOC:	"+resultMap.get(methodName);
					}
				}
				ispis.append(classReport+methodReport);
			}
		});
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ispis.removeAll();
				HashMap<String,Integer> resultMap = new HashMap<String, Integer>();
		    	
				McCabe mccabe = new McCabe();
				File sourceFile = new File(putanja.getText());
				try {
					resultMap = mccabe.getMcCabe(sourceFile);
				} catch (Exception e) {
					e.printStackTrace();
				}	
				
				String methodReport="\nMcCabe Izvjestaj kompleksnosti:";
				String classReport="";
				for(String methodName: resultMap.keySet()){
					if(methodName=="Class"){
						String name=sourceFile.getName();
						String className=name.substring(0, name.indexOf("."));		
						classReport+="McCabe kompleksnost klase "+ className;
						classReport+=" je "+resultMap.get("Class");
					}
					else{
						methodReport+="\nMetoda:	"+methodName;
						methodReport+="\nKompleksnost:	"+resultMap.get(methodName);
					}
				}
				ispis.append(classReport+methodReport);
			}
		});
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
				
				   int returnVal = fc.showOpenDialog(MainForma.this);

			      if (returnVal == JFileChooser.APPROVE_OPTION) {
				        File file = fc.getSelectedFile();
				        putanja.setText(file.getPath());
				        
			      } else {
			    	  	putanja.setText("Ukinuto otvaranje");
			      }
			      putanja.setCaretPosition(putanja.getDocument().getLength());
			    } 
		});
	}
}
